﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mommosoft.ExpertSystem;

namespace WineDemo {
    public partial class MainForm : Form {
        private class CourseList {
            public string Course { get; set; }
            public float Certainty { get; set; }
        }
        private class CourseSchedule
        {
            public string Course { get; set; }
            public float Certainty { get; set; }
            public string Session { get; set; }
        }

        string newline = "\r\n \r\n";

        string[] trackChoices = { "Software Engineering", "Knowledge Engineering" };
        string[] keChoices = { "Business Analytics", "Text Mining", "Genetic Algorithms", "Case Based Reasoning", "Knowledge Management" };
        string[] interestChoices = { "Don't know", "Requirements, Design & Construction", "Software Development Platforms & Technologies", 
                                       "IT Infrastructure Technology", "Advanced IT Management", 
                                       "Technopreneurship & Innovation", "Knowledge Engineering Techniques" };
        string[] skillsetChoices = { "Don't know", "Developer", "Analyst", "Architect", "PM", "IT Manager", "Network / Security", "Others" };

        private Mommosoft.ExpertSystem.Environment enviroment;
        private bool ready = false;

        public MainForm() {
            InitializeComponent();

            dataGridView.AutoGenerateColumns = false;

            trackComboBox.DataSource = trackChoices;

            interest1ComboBox.BindingContext = new BindingContext();//so selectedindex wont affect all
            interest2ComboBox.BindingContext = new BindingContext();
            interest3ComboBox.BindingContext = new BindingContext();

            interest1ComboBox.DataSource = interestChoices;
            interest2ComboBox.DataSource = interestChoices;
            interest3ComboBox.DataSource = interestChoices;
 
            skillset1ComboBox.BindingContext = new BindingContext();//so selectedindex wont affect all
            skillset2ComboBox.BindingContext = new BindingContext();
            skillset3ComboBox.BindingContext = new BindingContext();
            
            skillset1ComboBox.DataSource = skillsetChoices;
            skillset2ComboBox.DataSource = skillsetChoices;
            skillset3ComboBox.DataSource = skillsetChoices;

            enviroment = new Mommosoft.ExpertSystem.Environment();
            enviroment.Clear();

            enviroment.Load("templates_rules_none.clp");
            enviroment.Load("predef_facts.clp");
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            RunWine();

            ready = true;
        }
        private void RunWine() {
            enviroment.Reset();
            Console.WriteLine("(reset)");

            string item = (string)trackComboBox.SelectedValue;

            if (item.Equals("Knowledge Engineering"))
            {
                enviroment.AssertString("(student-track ke)");
                Console.WriteLine("(assert (student-track ke))");
                if (keComboBox.SelectedValue != null)
                {
                    enviroment.AssertString("(student_least_favourite_ke \"" + keComboBox.SelectedValue.ToString() + "\")");
                    Console.WriteLine("(assert (student_least_favourite_ke \"" + keComboBox.SelectedValue.ToString() + "\"))");
                }
            }
            else
            {
                enviroment.AssertString("(student-track se)");//dun think this does anything
                Console.WriteLine("(assert (student-track se))");
            }

            float careerPreference = (float)preferenceCareerTrackBar.Value / 100;
            float interestPreference = (float)preferenceInterestTrackBar.Value / 100;

            enviroment.AssertString("(course-purpose-career-rating " + careerPreference + ")");
            Console.WriteLine("(assert (course-purpose-career-rating " + careerPreference + "))");
            enviroment.AssertString("(course-purpose-interest-rating " + interestPreference + ")");
            Console.WriteLine("(assert (course-purpose-interest-rating " + interestPreference + "))");
                
            float careerTechnical = (float)careerTechnicalTrackBar.Value / 100;
            enviroment.AssertString("(career-goal-tech-inclination " + careerTechnical + ")");
            Console.WriteLine("(assert (career-goal-tech-inclination " + careerTechnical + "))");

            float careerManagement = (float)careerManagementTrackBar.Value / 100;
            enviroment.AssertString("(career-goal-management-inclination " + careerManagement + ")");
            Console.WriteLine("(assert (career-goal-management-inclination " + careerManagement + "))");
 
            int skillset1 = skillset1ComboBox.SelectedIndex;
            int skillset2 = skillset2ComboBox.SelectedIndex;
            int skillset3 = skillset3ComboBox.SelectedIndex;
            if (skillset1 != 0 && skillset2 != 0 && skillset3 != 0)
            {
                enviroment.AssertString("(jobroles_string \"" + skillset1 + " " + skillset2 + " " + skillset3 + "\")");
                Console.WriteLine("(assert (jobroles_string \"" + skillset1 + " " + skillset2 + " " + skillset3 + "\"))");
            }

            int interest1 = interest1ComboBox.SelectedIndex;
            int interest2 = interest2ComboBox.SelectedIndex;
            int interest3 = interest3ComboBox.SelectedIndex;
            if (interest1 != 0 && interest2 != 0 && interest3 != 0)
            {
                enviroment.AssertString("(interests_string \"" + interest1 + " " + interest2 + " " + interest3 + "\")");
                Console.WriteLine("(assert (interests_string \"" + interest1 + " " + interest2 + " " + interest3 + "\"))");
            }

            enviroment.Run();
            Console.WriteLine("(run)");

            UpdateCourseList();
        }

        private void UpdateCourseList()
        {
            string evalStr = "(COURSES::get-course-list)";

            MultifieldValue mv = (MultifieldValue)enviroment.Eval(evalStr);

            List<CourseList> recCourseList = new List<CourseList>();

            for (int i = 0; i < mv.Count; i++)
            {
                FactAddressValue fv = (FactAddressValue)mv[i];

                float certainty = (float)((FloatValue)fv.GetFactSlot("cf"));

                String Course = ((StringValue)fv.GetFactSlot("goal"));

                recCourseList.Add(new CourseList() { Course = Course, Certainty = certainty });
            }

            dataGridView.DataSource = recCourseList;

            evalStr = "(COURSES::get-course-schedule)";

            MultifieldValue mv1 = (MultifieldValue)enviroment.Eval(evalStr);

            List<CourseSchedule> recCourseSchedule = new List<CourseSchedule>();

            year1sem1Label.Text = "1st Year, First Semester \r\n";
            year1sem2Label.Text = "1st Year, Second Semester \r\n";
            year2sem1Label.Text = "2nd Year, First Semester \r\n";
            year2sem2Label.Text = "2nd Year, Second Semester \r\n";

            for (int i = 0; i < mv1.Count; i++)
            {
                FactAddressValue fv = (FactAddressValue)mv1[i];

                String session = (String)((SymbolValue)fv.GetFactSlot("SID"));

                String Course = ((StringValue)fv.GetFactSlot("NAME"));

                float certainty = (float)((FloatValue)fv.GetFactSlot("CF"));

                recCourseSchedule.Add(new CourseSchedule() { Course = Course, Session = session, Certainty = certainty });

                if (session.Equals("S1") || session.Equals("S2"))
                {
                    year1sem1Label.Text += newline + Course;
                }
                if (session.Equals("S3") || session.Equals("S4"))
                {
                    year1sem2Label.Text += newline + Course;
                }
                if (session.Equals("S5") || session.Equals("S6"))
                {
                    year2sem1Label.Text += newline + Course;
                }
                if (session.Equals("S7") || session.Equals("S8"))
                {
                    year2sem2Label.Text += newline + Course;
                }
            }
        }

        private void OnChange(object sender, EventArgs e) {
            if (ready) {
                RunWine();
            }
        }

        private void preferenceTrackBar_Scroll(object sender, EventArgs e)
        {
            toolTip1.SetToolTip(preferenceCareerTrackBar, preferenceCareerTrackBar.Value.ToString());
            if (preferenceCareerTrackBar.Value == 0)
            {
                careerTechnicalTrackBar.Enabled = false;
                careerManagementTrackBar.Enabled = false;
                careerTechnicalTrackBar.Value = 0;
                careerManagementTrackBar.Value = 0;
            }
            else
            {
                careerTechnicalTrackBar.Enabled = true;
                careerManagementTrackBar.Enabled = true;
            }
        }

        private void preferenceInterestTrackBar_Scroll(object sender, EventArgs e)
        {
            toolTip1.SetToolTip(preferenceInterestTrackBar, preferenceInterestTrackBar.Value.ToString());
            if (preferenceInterestTrackBar.Value == 0)
            {
                interest1ComboBox.Enabled = false;
                interest2ComboBox.Enabled = false;
                interest3ComboBox.Enabled = false;
                interest1ComboBox.SelectedIndex = 0;
                interest2ComboBox.SelectedIndex = 0;
                interest3ComboBox.SelectedIndex = 0;
            }
            else
            {
                interest1ComboBox.Enabled = true;
                interest2ComboBox.Enabled = true;
                interest3ComboBox.Enabled = true;
            }
        }

        private void careerTechnicalTrackBar_Scroll(object sender, EventArgs e)
        {
            toolTip1.SetToolTip(careerTechnicalTrackBar, careerTechnicalTrackBar.Value.ToString());
        }

        private void careerManagementTrackBar_Scroll(object sender, EventArgs e)
        {
            toolTip1.SetToolTip(careerManagementTrackBar, careerManagementTrackBar.Value.ToString());
        }

        private void trackComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (trackComboBox.SelectedItem.Equals("Knowledge Engineering"))
            {
                keComboBox.DataSource = keChoices;
                keComboBox.Enabled = true;
            }
            else
            {
                keComboBox.Enabled = false;
            }
        }
    }
}
